local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local Map = ____lualib.Map
local __TS__New = ____lualib.__TS__New
local __TS__ArraySplice = ____lualib.__TS__ArraySplice
local __TS__ArrayForEach = ____lualib.__TS__ArrayForEach
local ActorEventDispatcher = __TS__Class()
ActorEventDispatcher.name = "ActorEventDispatcher"
function ActorEventDispatcher.prototype.____constructor(self)
    self.dispatcher = __TS__New(Map)
end
function ActorEventDispatcher.prototype.ActorEventDispatcher(self)
    _G.OnTriggerEvent = function(...) return self:OnTriggerEvent(...) end
end
function ActorEventDispatcher.prototype.regEventToTable(self, tab, eventId, proc)
    if not tab:has(eventId) then
        tab:set(eventId, {})
    end
    local procs = tab:get(eventId)
    for i = 0, #procs, 1 do
        if procs[i + 1] == proc then
            assert(
                false,
                "event already registered for event id: " .. tostring(eventId)
            )
            return
        end
    end
    procs[#procs + 1] = proc
end
function ActorEventDispatcher.prototype.unRegEventToTable(self, tab, eventId, proc)
    if not tab:has(eventId) then
        return
    end
    local procs = tab:get(eventId)
    for i = #procs - 1, 0, -1 do
        if procs[i + 1] == proc then
            __TS__ArraySplice(procs, i, 1)
            return
        end
    end
end
function ActorEventDispatcher.prototype.regEvent(self, eventId, proc)
    if self.dispatcher:has(eventId) then
        assert(false, "event already registered")
        return
    end
    if proc == nil then
        assert(
            false,
            "proc is null,event id is: " .. tostring(eventId)
        )
        return
    end
    self:regEventToTable(self.dispatcher, eventId, proc)
end
function ActorEventDispatcher.prototype.unRegEvent(self, eventId, proc)
    if not self.dispatcher:has(eventId) then
        return
    end
    self:unRegEventToTable(self.dispatcher, eventId, proc)
end
function ActorEventDispatcher.prototype.OnTriggerEvent(self, actor, event_id, ...)
    local args = {...}
    local handlers = self.dispatcher:get(event_id)
    if not handlers then
        print("未注册的事件 ID: " .. tostring(event_id))
        return
    end
    __TS__ArrayForEach(
        handlers,
        function(____, handler)
            handler(
                actor,
                table.unpack(args)
            )
        end
    )
end
local ____exports = __TS__New(ActorEventDispatcher)
return ____exports
